package DB;

import Model.*;

import java.sql.*;
import java.util.ArrayList;

public class DBFormat 
{
	private DBVare dbVar;
	private DBUdlej_Salg_Vare dbUsv;
	private Connection con;
	private DBEksemplar dbEks;
	
private static DBFormat  instance = null;
    
    public static DBFormat getInstance()
    {
        if (instance == null)
        {
          instance = new DBFormat();
        }
        return instance;
    }
	
	private DBFormat()
	{
		dbVar = DBVare.getInstance();
		dbUsv = DBUdlej_Salg_Vare.getInstance();
		
		con = DBConnection.getInstance().getDBcon();
	}
	
	public ArrayList<Format> getAlleFormater()
	{
		return miscWhere("");
	}
	
	private Format singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		Format format = new Format();
	    String query =  buildQuery(wClause);
		try{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() ){
	 				format = buildFormat(results, retrieveAssociation);
                            stmt.close();	                            
			}
		}
	 	catch(Exception e){
	 		System.out.println("Query exception: "+e);
	 	}
		return format;
	}
	
	private ArrayList<Format> miscWhere(String wClause)
	{
		ResultSet results;
		ArrayList<Format> list = new ArrayList<Format>();	
		
		String query =  buildQuery(wClause);
  
  		try
  		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);		 		

			while( results.next() )
			{
		     	 Format format = new Format();
		     	 format = buildFormat(results, true);	
                 list.add(format);	
			}
			stmt.close();
		}
	 	catch(Exception e){
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return list;
	}
	
	private String buildQuery(String wClause)
	{
	    String query="SELECT * FROM Format";
		
		if (wClause.length()>0)
			query=query+" WHERE "+ wClause;
			
		return query;
	}
	
	private Format buildFormat(ResultSet results, boolean rA)
    {   
		DBFormat dbForm = new DBFormat();
		Format format = new Format();
		dbEks = DBEksemplar.getInstance();
          try
          {
        	  	format.setFormatid(results.getInt(1)); 
                format.setMedie(results.getString(2));
                format.setAntal(results.getInt(3));
                format.setPris(results.getDouble(4));
                format.setVare(dbVar.findVareId(results.getInt(5), false));
                if(rA==true)
                {
                    format.setEks(dbEks.findEksemplarViaFormat(results.getInt(1),true));
                    format.setUsv(dbUsv.findUsvViaFormat(results.getInt(1), false));
                }            
          }
         catch(Exception e)
         {
             System.out.println("error in building the format object" + e);
         }
         return format;
    }
	
	private void create(String wClause)
	{
	    String query="INSERT INTO Format ";
	   
		if (wClause.length()>0)
		{
			query=query+ wClause;
			
			Statement stmt;
			String sql;     
			int rows;
			
			try
			{
			stmt = con.createStatement(); 
			rows = stmt.executeUpdate(query); 
			con.commit(); 
			stmt.close();
			}
			catch (Exception e)
			{ 
			  e.printStackTrace(); 
			}
		}
		else
		{
			System.out.println("Insert variables");
		}
	}
	
	public void createFormat(String medie, int antal)
	{
			String wClause = "VALUES " 
            + "('" + medie   + "',"
            + "'" + antal + "',)";
            create(wClause);
	}
	
	public void updateFormat(Format format)
	{
		
		PreparedStatement prepstmt;
		try 
		{     
		  prepstmt = con.prepareStatement
		   ("UPDATE Format SET " 
				+ "Medie = '" + format.getMedie() + "'"
		   		+ "Antal = '" + format.getAntal() + "'"
		   		+ " WHERE RabatId = " + format.getFormatid() + "");
		  prepstmt.executeUpdate();
		             
		  con.commit();
		  prepstmt.close();
		}
		catch (Exception e) 
		{
		  e.printStackTrace();
		}
	}
	
	public void deleteFormat(String navn)
	{
		PreparedStatement prepstmt;
		try 
		{     
		  prepstmt = con.prepareStatement
		    ("DELETE FROM Format WHERE medie = " + navn + "");   
		  prepstmt.executeUpdate();          
		  con.commit();
		  prepstmt.close();
		}
		catch (Exception e) 
		{
		  e.printStackTrace();
		}
	}
	
	public Format findFormat(int id, boolean rA) 
    {
		String wClause = " formatid = " + id + "";
    	return singleWhere(wClause, rA);
    }
	
	public ArrayList<Format> findFormatViaVare(int vareid, boolean rA)
	{
		String query = "SELECT * FROM Format where vareid =" + vareid;
		ResultSet results;
		ArrayList<Format> list = new ArrayList<Format>();	
  
  		try
  		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
			while(results.next())
			{
				Format f = new Format();
				f = buildFormat(results, rA);	
                list.add(f);
			}
			stmt.close();
		}
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return list;
	}
}
